package com.hsgene.user.dto.login;

import com.hsgene.common.util.basic.SimpleRegexUtils;
import com.hsgene.common.util.exception.ErrorCode;
import com.hsgene.common.util.validation.FieldChecker;
import com.hsgene.user.constants.CustomerSourceType;
import com.hsgene.user.exception.UserErrorCode;

import java.io.Serializable;

/**
 * 用户登录信息
 *
 * @author wangbing
 * @version 1.0, 2018/10/16
 */
public class UserLoginInfoDto implements Serializable {

    private static final long serialVersionUID = -4491482972948214025L;

    public static final Integer SMS_LOGIN = 1;
    public static final Integer PASSWORD_LOGIN = 2;

    /**
     * 登录类型：1-短信验证码登录，2-密码登录
     */
    private Integer loginType = 1;

    /**
     * 登录账号名
     */
    private String userName;

    /**
     * 短信验证码
     */
    private String smsCode;

    /**
     * 密码
     */
    private String password;

    /**
     * 登录端别（来源类型：APP,WEB,wechat_application等）
     */
    private String sourceType = "APP";

    public ErrorCode check() {
        if (SMS_LOGIN.equals(loginType)) {
            FieldChecker.checkEmpty(userName, "电话号码");
            // 校验手机号码格式
            if (!SimpleRegexUtils.isMobile(userName)) {
                return UserErrorCode.USER_PHONE_NOT_FORMAT;
            }
            FieldChecker.checkEmpty(smsCode, "登录验证码");
        } else {
            FieldChecker.checkEmpty(userName, "账号名");
            FieldChecker.checkEmpty(password, "登录密码");
        }
        return null;
    }

    public CustomerSourceType parseSourceType() {
        return CustomerSourceType.getEnum(sourceType);
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public Integer getLoginType() {
        return loginType;
    }

    public void setLoginType(Integer loginType) {
        this.loginType = loginType;
    }

    public String getSmsCode() {
        return smsCode;
    }

    public void setSmsCode(String smsCode) {
        this.smsCode = smsCode;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getSourceType() {
        return sourceType;
    }

    public void setSourceType(String sourceType) {
        this.sourceType = sourceType;
    }
}
